package edu.purdue.spm.component;

import java.util.Date;

import edu.purdue.spm.Login;
import edu.purdue.spm.MainDashBoardFragment;
import edu.purdue.spm.R;
import edu.purdue.spm.entity.Task;
import edu.purdue.spm.util.Portal;
import edu.purdue.spm.util.StopWatch;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.text.InputType;
import android.util.Log;
import android.view.DragEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnDragListener;
import android.view.View.OnFocusChangeListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.NumberPicker;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;
import android.widget.TextView;
import android.widget.EditText;
import android.widget.Toast;

public class TaskInformationPanelFragment extends Fragment{

	static View base;
	static Task selectedTaskDetail;

	public void setTaskDetail(Task taskDetail){
		this.selectedTaskDetail = taskDetail;
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		// Inflate the layout for this fragment
		Log.i("MSG", "TaskInformationPanelFragment onCreateView !!!");
		base = inflater.inflate(R.layout.task_detail_information_panel, container, false);


		populateTaskSpecPanel();

		final SeekBar progressUpdateBar = (SeekBar) base.findViewById(R.id.personal_task_Progress_SeekBar);
		progressUpdateBar.setFocusable(true);
		progressUpdateBar.setFocusableInTouchMode(true);

		progressUpdateBar.setOnSeekBarChangeListener(new OnSeekBarChangeListener(){

			@Override
			public void onProgressChanged(SeekBar arg0, int arg1, boolean arg2) {
				// TODO Auto-generated method stub
				((TextView)base.findViewById(R.id.personal_task_progress_monitor_TextView)).setText("Progress: "+progressUpdateBar.getProgress()+"%");
			}

			@Override
			public void onStartTrackingTouch(SeekBar arg0) {
				// TODO Auto-generated method stub
				((TextView)base.findViewById(R.id.personal_task_progress_monitor_TextView)).setText("Progress: "+progressUpdateBar.getProgress()+"%");
			}

			@Override
			public void onStopTrackingTouch(SeekBar arg0) {
				// TODO Auto-generated method stub
				((TextView)base.findViewById(R.id.personal_task_progress_monitor_TextView)).setText("Progress: "+progressUpdateBar.getProgress()+"%");
			}

		});

		Button deleteTaskButton = (Button)base.findViewById(R.id.delete_task_button);


		Button updateTaskProgressButton = (Button)base.findViewById(R.id.update_taskProgress_button);

		updateTaskProgressButton.setOnClickListener(new Button.OnClickListener(){

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				selectedTaskDetail.setProgress(progressUpdateBar.getProgress() / 100.0);
				((TextView)base.findViewById(R.id.taskprogress_textview)).setText((int)(selectedTaskDetail.getProgress()*100)+"%");
				Date dueDate = new Date(selectedTaskDetail.getDueTime().toMillis(false));

				String dueTime = String.format("%d-%02d-%02d %02d:%02d:%02d", 1900+dueDate.getYear(), dueDate.getMonth()+1, dueDate.getDate(), dueDate.getHours(), dueDate.getMinutes(), dueDate.getSeconds());			

				String request = "ModifyTask:;:"+Login.GCMaccount+":;:<'"+selectedTaskDetail.getTaskID()+"':.:"+null+":.:'"+Login.GCMaccount+"':.:'"+selectedTaskDetail.getTitle()+
						"':.:'"+selectedTaskDetail.getDiscription()+"':.:'"+dueTime+"':.:"+selectedTaskDetail.getProgress()+":.:'P':.:1.0:.:'L'>";

				Portal.messageBack = null;
				Portal.sendMessageGivenContext(base.getContext(), request);
				Toast.makeText(base.getContext(), Portal.messageBack, Toast.LENGTH_SHORT).show();
				MainDashBoardFragment.refreshTaskList();
			}

		});


		deleteTaskButton.setOnClickListener(new Button.OnClickListener(){

			@Override
			public void onClick(View v) {
				String request = "DeleteTask:;:"+Login.GCMaccount+":;:'"+selectedTaskDetail.getTaskID()+"'";
				Log.i("HOHOHO", "Request is: "+request);
				Portal.messageBack = null;
				Portal.sendMessageGivenContext(base.getContext(), request);

				while(Portal.messageBack == null){}

				Log.i("TRACK on delete task", Portal.messageBack);

				Toast.makeText(base.getContext(), Portal.messageBack, Toast.LENGTH_LONG).show();

				int i = 0;
				for(i=0; i<MainDashBoardFragment.fullTaskList.size(); i++){
					if (MainDashBoardFragment.fullTaskList.get(i).getTaskID().equals(selectedTaskDetail.getTaskID())){
						MainDashBoardFragment.fullTaskList.remove(i);
						break;
					}
				}
				MainDashBoardFragment.refreshTaskList();
			}

		});
		return base;
	}

	public static void populateTaskSpecPanel(){

		((EditText)base.findViewById(R.id.tasktitle_EditText)).setText(selectedTaskDetail.getTitle());
		((EditText)base.findViewById(R.id.tasktitle_EditText)).setFocusable(false);
		((TextView)base.findViewById(R.id.taskowner_textview)).setText(selectedTaskDetail.getOwnerID());

		((TextView)base.findViewById(R.id.dateassigned_textview)).setText(StopWatch.getTimeInLocale(selectedTaskDetail.getTimeStamp()));

		((TextView)base.findViewById(R.id.duetime_textview)).setText(StopWatch.getTimeInLocale(selectedTaskDetail.getDueTime()));

		((TextView)base.findViewById(R.id.taskprogress_textview)).setText((int)(selectedTaskDetail.getProgress()*100)+"%");

		((EditText)base.findViewById(R.id.taskcontent_detail_EditText)).setText(selectedTaskDetail.getDiscription());
		((EditText)base.findViewById(R.id.taskcontent_detail_EditText)).setFocusable(false);
		
		((SeekBar) base.findViewById(R.id.personal_task_Progress_SeekBar)).setProgress((int)(selectedTaskDetail.getProgress()*100));
		((TextView)base.findViewById(R.id.personal_task_progress_monitor_TextView)).setText("Progress: "+(int)(selectedTaskDetail.getProgress()*100)+"%");
	}
}
